package com.lez.kotlincoroutine.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.lez.kotlincoroutine.api.Banner
import com.lez.kotlincoroutine.repositroy.BannerRepositroy
import kotlinx.coroutines.launch

class MainViewModel(): ViewModel() {

    //定义一个LiveData
    var bannerLiveData = MutableLiveData<Banner>()

    //初始化Repositroy
    val bannerRepositroy= BannerRepositroy()

    //使用viewModelScope来定义协程作用域
    fun loadBanner(){
       viewModelScope.launch {
           bannerLiveData.value = bannerRepositroy.loadBanners()
       }
    }

}